using System;
using System.Collections.Generic;
using Volo.Abp.Application.Dtos;
using Matrix.ServiceDiscovery.Enums;

namespace Matrix.ServiceDiscovery.Services
{
    /// <summary>
    /// 健康检查记录DTO
    /// </summary>
    public class HealthCheckRecordDto : CreationAuditedEntityDto<Guid>
    {
        /// <summary>
        /// 服务实例ID
        /// </summary>
        public Guid ServiceInstanceId { get; set; }

        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 实例ID
        /// </summary>
        public string InstanceId { get; set; } = string.Empty;

        /// <summary>
        /// 检查类型
        /// </summary>
        public HealthCheckType CheckType { get; set; } = HealthCheckType.Http;

        /// <summary>
        /// 检查端点
        /// </summary>
        public string CheckEndpoint { get; set; } = string.Empty;

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus HealthStatus { get; set; } = HealthStatus.Unknown;

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public long ResponseTimeMs { get; set; }

        /// <summary>
        /// HTTP状态码
        /// </summary>
        public int? HttpStatusCode { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 检查详情
        /// </summary>
        public Dictionary<string, object>? CheckDetails { get; set; }

        /// <summary>
        /// 检查时间
        /// </summary>
        public DateTime CheckTime { get; set; }

        /// <summary>
        /// 检查耗时（毫秒）
        /// </summary>
        public long CheckDurationMs { get; set; }

        /// <summary>
        /// 检查器标识
        /// </summary>
        public string CheckerId { get; set; } = string.Empty;

        /// <summary>
        /// 检查器类型
        /// </summary>
        public string CheckerType { get; set; } = string.Empty;

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess => HealthStatus == HealthStatus.Healthy;

        /// <summary>
        /// 性能等级
        /// </summary>
        public PerformanceLevel PerformanceLevel { get; set; }

        /// <summary>
        /// 响应时间描述
        /// </summary>
        public string ResponseTimeDescription => PerformanceLevel switch
        {
            PerformanceLevel.Excellent => "优秀 (<100ms)",
            PerformanceLevel.Good => "良好 (100-300ms)",
            PerformanceLevel.Acceptable => "可接受 (300-1000ms)",
            PerformanceLevel.Slow => "较慢 (1-5s)",
            PerformanceLevel.VerySlow => "很慢 (>5s)",
            PerformanceLevel.Failed => "失败",
            _ => "未知"
        };
    }

    /// <summary>
    /// 创建健康检查记录DTO
    /// </summary>
    public class CreateHealthCheckRecordDto
    {
        /// <summary>
        /// 服务实例ID
        /// </summary>
        public Guid ServiceInstanceId { get; set; }

        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 实例ID
        /// </summary>
        public string InstanceId { get; set; } = string.Empty;

        /// <summary>
        /// 检查类型
        /// </summary>
        public HealthCheckType CheckType { get; set; } = HealthCheckType.Http;

        /// <summary>
        /// 检查端点
        /// </summary>
        public string CheckEndpoint { get; set; } = string.Empty;

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus HealthStatus { get; set; } = HealthStatus.Unknown;

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public long ResponseTimeMs { get; set; }

        /// <summary>
        /// HTTP状态码
        /// </summary>
        public int? HttpStatusCode { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 检查详情
        /// </summary>
        public Dictionary<string, object>? CheckDetails { get; set; }

        /// <summary>
        /// 检查耗时（毫秒）
        /// </summary>
        public long CheckDurationMs { get; set; }

        /// <summary>
        /// 检查器标识
        /// </summary>
        public string CheckerId { get; set; } = string.Empty;

        /// <summary>
        /// 检查器类型
        /// </summary>
        public string CheckerType { get; set; } = string.Empty;
    }

    /// <summary>
    /// 更新健康检查记录DTO
    /// </summary>
    public class UpdateHealthCheckRecordDto
    {
        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus? HealthStatus { get; set; }

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public long? ResponseTimeMs { get; set; }

        /// <summary>
        /// HTTP状态码
        /// </summary>
        public int? HttpStatusCode { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }

        /// <summary>
        /// 检查详情
        /// </summary>
        public Dictionary<string, object>? CheckDetails { get; set; }

        /// <summary>
        /// 检查耗时（毫秒）
        /// </summary>
        public long? CheckDurationMs { get; set; }
    }

    /// <summary>
    /// 负载均衡策略枚举DTO（用于API响应）
    /// </summary>
    public static class LoadBalancingStrategyExtensions
    {
        public static string GetDescription(this LoadBalancingStrategy strategy)
        {
            return strategy switch
            {
                LoadBalancingStrategy.RoundRobin => "轮询",
                LoadBalancingStrategy.WeightedRoundRobin => "加权轮询",
                LoadBalancingStrategy.Random => "随机",
                LoadBalancingStrategy.WeightedRandom => "加权随机",
                LoadBalancingStrategy.LeastConnections => "最少连接",
                LoadBalancingStrategy.ResponseTime => "响应时间",
                LoadBalancingStrategy.ConsistentHash => "一致性哈希",
                LoadBalancingStrategy.IpHash => "IP哈希",
                _ => "未知"
            };
        }
    }

    /// <summary>
    /// 服务类型枚举DTO（用于API响应）
    /// </summary>
    public static class ServiceTypeExtensions
    {
        public static string GetDescription(this ServiceType serviceType)
        {
            return serviceType switch
            {
                ServiceType.WebApi => "Web应用",
                ServiceType.Api => "API服务",
                ServiceType.Worker => "后台工作",
                ServiceType.Database => "数据库",
                ServiceType.Cache => "缓存",
                ServiceType.MessageQueue => "消息队列",
                ServiceType.Gateway => "网关",
                ServiceType.Configuration => "配置中心",
                ServiceType.Monitoring => "监控服务",
                ServiceType.Logging => "日志服务",
                _ => "未知"
            };
        }
    }

    /// <summary>
    /// 健康检查类型枚举DTO（用于API响应）
    /// </summary>
    public static class HealthCheckTypeExtensions
    {
        public static string GetDescription(this HealthCheckType checkType)
        {
            return checkType switch
            {
                HealthCheckType.Http => "HTTP检查",
                HealthCheckType.Https => "HTTPS检查",
                HealthCheckType.Tcp => "TCP检查",
                HealthCheckType.Grpc => "gRPC检查",
                HealthCheckType.Script => "脚本检查",
                HealthCheckType.Heartbeat => "心跳检查",
                HealthCheckType.Database => "数据库检查",
                HealthCheckType.Cache => "缓存检查",
                HealthCheckType.MessageQueue => "消息队列检查",
                _ => "未知"
            };
        }
    }

    /// <summary>
    /// 服务状态枚举DTO（用于API响应）
    /// </summary>
    public static class ServiceStatusExtensions
    {
        public static string GetDescription(this ServiceStatus status)
        {
            return status switch
            {
                ServiceStatus.Registered => "已注册",
                ServiceStatus.Running => "运行中",
                ServiceStatus.Offline => "离线",
                ServiceStatus.Unhealthy => "健康检查失败",
                ServiceStatus.Disabled => "已禁用",
                ServiceStatus.Maintenance => "维护中",
                _ => "未知"
            };
        }

        public static string GetColor(this ServiceStatus status)
        {
            return status switch
            {
                ServiceStatus.Running => "green",
                ServiceStatus.Registered => "blue",
                ServiceStatus.Offline => "orange",
                ServiceStatus.Unhealthy => "red",
                ServiceStatus.Disabled => "gray",
                ServiceStatus.Maintenance => "yellow",
                _ => "default"
            };
        }
    }

    /// <summary>
    /// 健康状态枚举DTO（用于API响应）
    /// </summary>
    public static class HealthStatusExtensions
    {
        public static string GetDescription(this HealthStatus status)
        {
            return status switch
            {
                HealthStatus.Healthy => "健康",
                HealthStatus.Unhealthy => "不健康",
                HealthStatus.Checking => "检查中",
                HealthStatus.Timeout => "超时",
                HealthStatus.ConnectionFailed => "连接失败",
                _ => "未知"
            };
        }

        public static string GetColor(this HealthStatus status)
        {
            return status switch
            {
                HealthStatus.Healthy => "green",
                HealthStatus.Checking => "blue",
                HealthStatus.Timeout => "orange",
                HealthStatus.Unhealthy => "red",
                HealthStatus.ConnectionFailed => "red",
                _ => "default"
            };
        }
    }
}